library(plm)
library(foreign)
library(stargazer)
library(lmtest)
library(car)
library(gplots)
library(sandwich)
library(RCurl)
library(gdata)
library(ggplot2)
library(readxl)
library(AER)
library(systemfit)
library(dplyr)
library(readxl)
library(pastecs)
library(gridExtra)
library(sjPlot)
library(sjmisc)
library(ggplot2)

Data <- read_excel("Democratic.xlsx")

Data1 <- read_excel("Global.xlsx")

# Main Specifications - Global

reg1 <- lm(netgini ~ nosystem + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg1)

reg2 <- lm(netgini ~ hegemonic + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg2)

reg3 <- lm(netgini ~ twoparty + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg3)

reg4 <- lm(netgini ~ multi + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg4)

reg5 <- lm(redis ~ nosystem + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg5)

reg6 <- lm(redis ~ hegemonic + pres + gdplog + growth + inflation + exports + employ + edu + life + communist + 
             as.factor(Year), data = Data1)
summary(reg6)

reg7 <- lm(redis ~ twoparty + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg7)

reg8 <- lm(redis ~ multi + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data1)
summary(reg8)

# Main Specifications, Hegemonic Plot

reg1 <- lm(netgini ~ hegemonic:hegeyear + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

reg2 <- lm(redis ~ hegemonic:hegeyear + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg2)

plot1 <- plot_model(reg1, type = "pred", terms = c("hegemonic", "hegeyear[0, 1, 2, 5, 10, 20, 30, 50]"), legend.title = "Years Hegemonic", 
                    ci.lvl = NA, line.size = .8, show.legend = TRUE) +
  ggtitle(NULL) +
  xlab("Hegemonic Party System") +
  ylab("Inequality") +
  theme_bw() +
  geom_line(size = 1.25, linetype = "longdash") +
  scale_x_continuous(breaks=c(0, 1))

plot1

plot2 <- plot_model(reg2, type = "pred", terms = c("hegemonic", "hegeyear[0, 1, 2, 5, 10, 20, 30, 50]"), legend.title = "Years Hegemonic", 
                    ci.lvl = NA, line.size = .8, show.legend = FALSE) +
  ggtitle(NULL) +
  xlab("Hegemonic Party System") +
  ylab("Redistribution") +
  theme_bw() +
  geom_line(size = 1.25, linetype = "longdash") +
  scale_x_continuous(breaks=c(0, 1)) +
  scale_y_reverse()

plot2

grid.arrange(plot1, plot2, nrow=1, ncol=2)

# Main Specifications - Democratic

reg1 <- lm(netgini ~ party + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

reg2 <- lm(netgini ~ Volatility + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg2)

reg3 <- lm(netgini ~ party*Volatility + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg3)

reg4 <- lm(netgini ~ multi*party*Volatility + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg4)

reg5 <- lm(redis ~ party + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg5)

reg6 <- lm(redis ~ Volatility + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg6)

reg7 <- lm(redis ~ party*Volatility + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg7)

reg8 <- lm(redis ~ multi*party*Volatility + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg8)

# Main Specifications - Multi-Party Interaction Plot

reg1 <- lm(netgini ~ multi*party*Volatility + pres + gdplog + growth + exports + inflation + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

plot1 <- plot_model(reg1, type = "pred", terms = c("multi[0, 1]", "party[0.50, 1.0]", "Volatility[0, .10, .25, .50]"), ci.lvl = NA,
                    line.size = .8, show.legend = TRUE, legend.title = "Party Inst.") +
  ggtitle(NULL) +
  xlab("Multi-Party System") +
  scale_x_continuous(breaks=c(0, 1)) +
  geom_line(size = 1.25, linetype = "longdash") +
  ylab("Inequality") +
  theme_bw() 

plot1

# Appendix - Bivariate Regressions - Global Dataset

reg1 <- lm(netgini ~ nosystem +
             as.factor(Year), data = Data1)
summary(reg1)

reg2 <- lm(netgini ~ hegemonic + 
             as.factor(Year), data = Data1)
summary(reg2)

reg3 <- lm(netgini ~ twoparty +
             as.factor(Year), data = Data1)
summary(reg3)

reg4 <- lm(netgini ~ multi +
             as.factor(Year), data = Data1)
summary(reg4)

reg5 <- lm(redis ~ nosystem +
             as.factor(Year), data = Data1)
summary(reg5)

reg6 <- lm(redis ~ hegemonic + 
             as.factor(Year), data = Data1)
summary(reg6)

reg7 <- lm(redis ~ twoparty +
             as.factor(Year), data = Data1)
summary(reg7)

reg8 <- lm(redis ~ multi +
             as.factor(Year), data = Data1)
summary(reg8)

stargazer(reg1, reg2, reg3, reg4, reg5, reg6, reg7, reg8, title="Party Systems, Inequality, and Redistribution, Global Dataset 1990-2020", 
          align=TRUE, omit.stat=c("LL", "ser", "f"), omit = c("Constant"), 
          no.space=TRUE, dep.var.labels.include = TRUE, dep.var.caption = "",
          notes = ".", notes.align = "l",
          notes.append = FALSE)

# Appendix - Bivariate Regressions - Democratic Dataset

reg1 <- lm(netgini ~ party +  
             as.factor(Year), data = Data)
summary(reg1)

reg2 <- lm(netgini ~ Volatility + 
             as.factor(Year), data = Data)
summary(reg2)

reg3 <- lm(netgini ~ party*Volatility + 
             as.factor(Year), data = Data)
summary(reg3)

reg4 <- lm(netgini ~ multi*party*Volatility + 
             as.factor(Year), data = Data)
summary(reg4)

reg5 <- lm(redis ~ party +  
             as.factor(Year), data = Data)
summary(reg5)

reg6 <- lm(redis ~ Volatility + 
             as.factor(Year), data = Data)
summary(reg6)

reg7 <- lm(redis ~ party*Volatility + 
             as.factor(Year), data = Data)
summary(reg7)

reg8 <- lm(redis ~ multi*party*Volatility + 
             as.factor(Year), data = Data)
summary(reg8)

stargazer(reg1, reg2, reg3, reg4, reg5, reg6, reg7, reg8, title="Party System Institutionalization, Inequality, and Redistribution, Democracy Dataset 1990-2020", 
          align=TRUE, omit.stat=c("LL", "ser", "f"), omit = c("Constant"), 
          no.space=TRUE, dep.var.labels.include = TRUE, dep.var.caption = "",
          notes = ".", notes.align = "l",
          notes.append = FALSE)

# Appendix - Robustness Check, Competing Theories - Party System Structure

reg1 <- lm(netgini ~ hegemonic + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

reg2 <- lm(netgini ~ twoparty + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg2)

reg3 <- lm(netgini ~ multi + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg3)

reg4 <- lm(redis ~ hegemonic + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg4)

reg5 <- lm(redis ~ twoparty + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg5)

reg6 <- lm(redis ~ multi + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg6)

stargazer(reg1, reg2, reg3, reg4, reg5, reg6, title="Party System Structure, Inequality, and Redistribution, Democratic Dataset 1990-2020", 
          align=TRUE, omit.stat=c("LL", "ser", "f"), omit = c("Constant"), 
          no.space=TRUE, dep.var.labels.include = TRUE, dep.var.caption = "",
          notes = ".", notes.align = "l",
          notes.append = FALSE)

# Appendix - Robustness Check, Competing Theories - Party System Institutionalization

reg1 <- lm(netgini ~ party + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

reg2 <- lm(netgini ~ Volatility + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg2)

reg3 <- lm(netgini ~ party*Volatility + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg3)

reg4 <- lm(netgini ~ multi*party*Volatility + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg4)

reg5 <- lm(redis ~ party  + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg5)

reg6 <- lm(redis ~ Volatility  + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg6)

reg7 <- lm(redis ~ party*Volatility  + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg7)

reg8 <- lm(redis ~ multi*party*Volatility  + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg8)

stargazer(reg1, reg2, reg3, reg4, reg5, reg6, reg7, reg8, title="Party System Institutionalization, Inequality, and Redistribution, Democratic Dataset 1990-2020", 
          align=TRUE, omit.stat=c("LL", "ser", "f"), omit = c("Constant"), 
          no.space=TRUE, dep.var.labels.include = TRUE, dep.var.caption = "",
          notes = ".", notes.align = "l",
          notes.append = FALSE)

# Appendix - Robustness Check, Competing Theories - Party System Institutionalization Figure

reg1 <- lm(redis ~ multi*party*Volatility + marketgini*dem + proleft + pres + gdplog + growth + exports + inflation + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

plot1 <- plot_model(reg1, type = "pred", terms = c("multi[0, 1]", "party[0.50, 1.0]", "Volatility[0, .10, .25, .50]"), ci.lvl = NA,
                    line.size = .8, show.legend = TRUE, legend.title = "Party Inst.") +
  ggtitle(NULL) +
  xlab("Multi-Party System") +
  scale_x_continuous(breaks=c(0, 1)) +
  geom_line(size = 1.25, linetype = "longdash") +
  ylab("Redistribution") +
  theme_bw() 

plot1

# Appendix - WIID Replication, Democratic Dataset

reg1 <- lm(widergini ~ hegemonic + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg1)

reg2 <- lm(widergini ~ twoparty + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg2)

reg3 <- lm(widergini ~ multi + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg3)

reg4 <- lm(widergini ~ party + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg4)

reg5 <- lm(widergini ~ Volatility + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg5)

reg6 <- lm(widergini ~ multi*party*Volatility + marketgini*dem + proleft + pres + gdplog + growth + inflation + exports + employ + edu + life + communist +
             as.factor(Year), data = Data)
summary(reg6)

stargazer(reg1, reg2, reg3, reg4, reg5, reg6, title="Party Systems, Inequality, and Redistribution, WIID Replication", 
          align=TRUE, omit.stat=c("LL", "ser", "f"), omit = c("Constant"), 
          no.space=TRUE, dep.var.labels.include = TRUE, dep.var.caption = "",
          notes = ".", notes.align = "l",
          notes.append = FALSE)